<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>Data-co-pilot 用户需求页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }

        .container {
            width: 80%;
            margin: 100px auto;
            padding: 20px;
            background: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 4px;
        }

        h2 {
            text-align: center;
            color: #333;
            margin-bottom: 20px;
        }

        .sql-display{
            margin-bottom: 20px;
            white-space: pre-wrap;
            background: #f8f8f8;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            min-height: 150px;
            resize: vertical;
        }

        #response {
            margin-bottom: 20px;
            white-space: pre-wrap;
            background: #f8f8f8;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            min-height: 30px;
            resize: vertical;
        }

        .sql-display:focus {
            outline: none;
            border-color: #5cb85c;
            box-shadow: 0 0 8px rgba(92, 184, 92, 0.2);
        }

        .button-container {
            display: flex;
            justify-content: center;
            gap: 10px; /* 增加按钮间距 */
        }

        button {
            padding: 10px 20px;
            background: #5cb85c;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        button:hover {
            background: #4cae4c;
        }

        #response {
            margin-top: 20px;
            white-space: pre-wrap;
            background: #f8f8f8;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>Welcome to Data-co-pilot</h2>
    <p>生成的SQL语句：</p>
    <div class="sql-display" id="sql-display"></div>
    <div class="button-container">
        <button id="execute-sql">执行SQL语句</button>
        <button id="return-test" onclick="setReturnFlagAndReturn()">返回</button>
    </div>
    <p>错误信息：</p>
    <div id="response"></div>
    <div>
        <p>注意事项：</p>
        <ul>
            <li>用户将需求清楚的表述</li>
            <li>尽量避免使用较为生僻的词语进行表述</li>
            <li>表达尽量简洁明了，通俗易懂</li>
        </ul>
    </div>
</div>
<script>
    function setReturnFlagAndReturn() {
        localStorage.setItem('returnFromIndex', 'true');
        window.location.href = '/test';
    }

    document.addEventListener('DOMContentLoaded', function() {
        const sqlQuery = localStorage.getItem('sqlQuery');
        if (sqlQuery) {
            document.getElementById('sql-display').innerText = sqlQuery;
        }

        document.getElementById('execute-sql').addEventListener('click', function() {
            if (sqlQuery) {
                fetch('/execute_sql/', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ cmd: sqlQuery })
                })
                .then(response => response.json())
                .then(data => {
                    if (data.error) {
                        document.getElementById('response').innerText = 'Error: ' + data.error;
                    } else {
                        localStorage.setItem('queryResults', JSON.stringify(data));
                        window.location.href = '/result/';
                    }
                    // 清除存储的SQL语句
                    localStorage.removeItem('sqlQuery');
                })
                .catch(error => {
                    document.getElementById('response').innerText = 'Error: ' + error;
                });
            }
        });
    });
</script>
</body>
</html>
